home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 12.8 KB | 562 lines | [TEXT/MPS ] |
- /*
- File: TestLinkedList.cp
-
- Contains: Tester for the linked list class
-
- Copyright: © 1991-1994 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef __TESTLINKEDLIST__
- #include "TestLinkedList.h"
- #endif
-
- /**********************************************************************
- ** PUBLIC Constructor/Destructor
- ***********************************************************************/
-
- Constructor(LinkedList)
- Destructor(LinkedList)
-
- /**********************************************************************
- ** Test methods
- ***********************************************************************/
-
- void TTestLinkedList::TestIsEmpty(Boolean correct) const
- {
- if (correct)
- {
- if (!fTest->IsEmpty())
- Printf("### ERROR: IsEmpty returned false when the list was not empty\n");
- }
- else
- {
- if (fTest->IsEmpty())
- Printf("### ERROR: IsEmpty returned true when the list was empty\n");
- }
- }
-
- void TTestLinkedList::TestCount(size_t shouldBe) const
- {
- size_t is = fTest->Count();
- if (is != shouldBe)
- Printf("### ERROR: Count returned %u when it should have returned %u\n",
- is, shouldBe);
- }
-
- void TTestLinkedList::TestFirstLast(short first, short last) const
- {
- TNumber* ptr;
- TLink* link;
-
- if ((ptr = (TNumber*)fTest->First()) != NULL)
- {
- if (ptr->fNumber != first)
- if (first < 0)
- Printf("### ERROR: First returned #%hu when it should have returned NULL\n",
- ptr->fNumber);
- else
- Printf("### ERROR: First returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, first);
- }
- else
- {
- if (first >= 0)
- Printf("### ERROR: First return NULL when it should have returned #%u\n",
- first);
- }
- if ((ptr = (TNumber*)fTest->Last()) != NULL)
- {
- if (ptr->fNumber != last)
- if (last < 0)
- Printf("### ERROR: Last returned #%hu when it should have returned NULL\n",
- ptr->fNumber);
- else
- Printf("### ERROR: Last returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, last);
- }
- else
- {
- if (last >= 0)
- Printf("### ERROR: Last return NULL when it should have returned #%u\n",
- last);
- }
- if ((link = fTest->FirstLink()) != NULL)
- {
- ptr = (TNumber*)link->GetValue();
- if (ptr->fNumber != first)
- if (first < 0)
- Printf("### ERROR: FirstLink returned #%hu when it should have returned NULL\n",
- ptr->fNumber);
- else
- Printf("### ERROR: FirstLink returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, first);
- }
- else
- {
- if (first >= 0)
- Printf("### ERROR: FirstLink return NULL when it should have returned #%u\n",
- first);
- }
- if ((link = fTest->LastLink()) != NULL)
- {
- ptr = (TNumber*)link->GetValue();
- if (ptr->fNumber != last)
- if (last < 0)
- Printf("### ERROR: LastLink returned #%hu when it should have returned NULL\n",
- ptr->fNumber);
- else
- Printf("### ERROR: LastLink returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, last);
- }
- else
- {
- if (last >= 0)
- Printf("### ERROR: LastLink return NULL when it should have returned #%u\n",
- last);
- }
- }
-
- void TTestLinkedList::TestMember(short number, Boolean ifGone) const
- {
- TNumber obj(number);
- TNumber* ptr;
- TLink* link;
-
- if ((ptr = (TNumber*)fTest->Member(obj)) != NULL)
- {
- if (ifGone)
- Printf("### ERROR: Member returned #%hu when it should not be present\n",
- ptr->fNumber);
- else
- if (number != ptr->fNumber)
- Printf("### ERROR: Member returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, number);
- }
- else
- if (!ifGone)
- Printf("### ERROR: Member returned no object when #%hu should have been present\n",
- number);
- if ((link = fTest->MemberLink(obj)) != NULL)
- {
- ptr = (TNumber*)link->GetValue();
- if (ifGone)
- Printf("### ERROR: MemberLink returned #%hu when it should not be present\n",
- ptr->fNumber);
- else
- if (number != ptr->fNumber)
- Printf("### ERROR: MemberLink returned #%hu when it should have returned #%hu\n",
- ptr->fNumber, number);
- }
- else
- if (!ifGone)
- Printf("### ERROR: MemberLink returned no object when #%hu should have been present\n",
- number);
- }
-
- /**********************************************************************
- ** PUBLIC InitTest
- ***********************************************************************/
-
- void TTestLinkedList::InitTest(BooleanParm verbose, BooleanParm, int, char**)
- {
- short idx;
-
- TStandardPool* pool = GetPool();
-
- for (idx = 0; idx < 100; ++idx)
- fArray[idx] = NULL;
-
- if (pool == NULL)
- {
- Printf("### ERROR: There is no global pool\n");
- return;
- }
-
- fTest = new (pool) TLinkedList;
-
- for (idx = 0; idx < 100; ++idx)
- {
- TLink* link;
- TNumber* num;
- if ((num = new (pool) TNumber(idx)) == NULL)
- {
- Printf("### ERROR: Out of Memory at #%d\n", idx);
- break;
- }
- if ((link = new (pool) TLink(num)) == NULL)
- {
- Printf("### ERROR: Out of Memory at #%d\n", idx);
- break;
- }
- fArray[idx] = link;
- }
- if (idx == 100 && verbose)
- Printf("INFO: Allocated 100 links and objects\n");
- }
-
- /**********************************************************************
- ** PUBLIC RunTestIteration
- ***********************************************************************/
-
- void TTestLinkedList::RunTestIteration(BooleanParm verbose, BooleanParm)
- {
- short matchFirst;
- short matchLast;
- short toMatch;
- short idx, jdx;
- TLink* link;
- TNumber* ptr;
- char* str;
-
- if (verbose)
- Printf("INFO: Testing EmptyList\n");
-
- TestIsEmpty(true);
- TestFirstLast(-1, -1);
- TestCount(0);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
-
- if (verbose)
- Printf("INFO: Testing AddLinkFirst, Member, Count, First, Last and IsEmpty\n");
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLinkFirst(link);
-
- TestIsEmpty(false);
- TestCount(idx + 1);
- TestFirstLast(idx, 0);
-
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, jdx > idx);
- }
-
- TestIsEmpty(false);
- TestCount(100);
- TestFirstLast(99, 0);
-
- matchFirst = 0;
- matchLast = 99;
- if (verbose)
- Printf("INFO: Testing First, Last, RemoveFirst, RemoveLast, and Member methods\n");
-
- for (idx = 0; idx < 100; ++idx)
- {
- TestIsEmpty(false);
- TestCount(100 - idx);
- TestFirstLast(matchLast, matchFirst);
-
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, jdx < matchFirst || jdx > matchLast);
-
- if (idx & 1)
- {
- link = fTest->RemoveFirstLink();
- toMatch = matchLast;
- matchLast -= 1;
- str = "RemoveFirstLink";
- }
- else
- {
- link = fTest->RemoveLastLink();
- toMatch = matchFirst;
- matchFirst += 1;
- str = "RemoveLastLink";
- }
- if (link == NULL)
- Printf("### ERROR: %s return no object, instead of #%hu\n",
- str, toMatch);
- else
- {
- ptr = (TNumber*)link->GetValue();
- if (ptr->fNumber != toMatch)
- Printf("### ERROR: %s return object #%hu, instead of #%hu\n",
- str, ptr->fNumber, toMatch);
- }
- }
-
- TestIsEmpty(true);
- TestFirstLast(-1, -1);
- TestCount(0);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
-
- if (verbose)
- Printf("INFO: Testing AddLinkLast, Member, Count, First, Last and IsEmpty\n");
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLinkLast(link);
-
- TestIsEmpty(false);
- TestCount(idx + 1);
- TestFirstLast(0, idx);
-
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, jdx > idx);
- }
-
- if (verbose)
- Printf("INFO: Testing Remove by pointer\n");
-
- matchFirst = 49;
- matchLast = 50;
-
- for (idx = 0; idx < 100; ++idx)
- {
- if (idx & 1)
- jdx = matchFirst;
- else
- jdx = matchLast;
- link = fArray[jdx];
-
- TestIsEmpty(false);
- TestCount(100 - idx);
- if (idx != 99)
- TestFirstLast(0, 99);
- else
- TestFirstLast(0, 0);
-
- if (!fTest->Remove(link->GetValue()))
- Printf("ERROR: Remove Link #%u Failed\n", jdx);
-
- TestCount(99-idx);
- if (idx < 98)
- TestFirstLast(0, 99);
- else
- if (idx == 98)
- TestFirstLast(0, 0);
-
- if (idx & 1)
- matchFirst -= 1;
- else
- matchLast += 1;
-
- }
- if (verbose)
- Printf("INFO: Testing Remove by reference\n");
-
- fTest->RemoveAll(); // Just in case
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLinkLast(link);
- }
- TestIsEmpty(false);
- TestCount(100);
- TestFirstLast(0, 99);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, false);
-
- matchFirst = 49;
- matchLast = 50;
-
- for (idx = 0; idx < 100; ++idx)
- {
- if (idx & 1)
- jdx = matchFirst;
- else
- jdx = matchLast;
- link = fArray[jdx];
-
- TestIsEmpty(false);
- TestCount(100 - idx);
- if (idx != 99)
- TestFirstLast(0, 99);
- else
- TestFirstLast(0, 0);
-
- if (!fTest->Remove(*(TNumber*)link->GetValue()))
- Printf("ERROR: Remove Link #%u Failed\n", jdx);
-
- TestCount(99-idx);
- if (idx < 98)
- TestFirstLast(0, 99);
- else
- if (idx == 98)
- TestFirstLast(0, 0);
-
- if (idx & 1)
- matchFirst -= 1;
- else
- matchLast += 1;
-
- }
-
- if (verbose)
- Printf("INFO: Testing Iterator\n");
-
- fTest->RemoveAll(); // Just in case
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLinkLast(link);
- }
-
- {
- TListIterator iter(fTest);
- TNumber* num;
- idx = 0;
- while ((num = (TNumber*)(&iter)->Next()) != NULL)
- {
- if (num->fNumber != idx)
- Printf("ERROR: Iterator returned #%u when #%u was expected\n",
- num->fNumber, idx);
- idx += 1;
- }
- if (idx != 100)
- Printf("ERROR: Iterator stopped after #%u\n", idx - 1);
-
- if (verbose)
- Printf("INFO: Testing RemoveCurrentObject at front\n");
- idx = 0;
- (&iter)->Reset();
- while ((num = (TNumber*)(&iter)->Next()) != NULL)
- {
- if (num->fNumber != idx)
- Printf("ERROR: Iterator returned #%u when #%u was expected\n",
- num->fNumber, idx);
- if (!(&iter)->RemoveCurrentObject())
- Printf("ERROR: RemoveCurrentObject returned false for #%u\n",
- idx);
- idx += 1;
- }
- if (idx != 100)
- Printf("ERROR: Iterator stopped after #%u\n", idx - 1);
- (&iter)->Reset();
- if ((num = (TNumber*)(&iter)->Next()) != NULL)
- Printf("ERROR: Iterator return #%u when list should be empty\n",
- num->fNumber);
- TestIsEmpty(true);
- TestCount(0);
- TestFirstLast(-1, -1);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
- fTest->RemoveAll(); // Just in case
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLinkLast(link);
- }
- if (verbose)
- Printf("INFO: Testing RemoveCurrentObject at end\n");
- for (jdx = 0; jdx < 100; ++jdx)
- {
- (&iter)->Reset();
- idx = 0;
- while ((num = (TNumber*)(&iter)->Next()) != NULL)
- {
- if (num->fNumber > 99-jdx)
- Printf("ERROR: Iterator returned #%u when it should be gone\n",
- num->fNumber);
- if (num->fNumber != idx)
- Printf("ERROR: Iterator returned #%u when #%u was expected\n",
- num->fNumber, idx);
- if (num->fNumber == 99-jdx && !(&iter)->RemoveCurrentObject())
- Printf("ERROR: RemoveCurrentObject returned false for #%u\n",
- idx);
- idx += 1;
- }
- if (idx != 100-jdx)
- Printf("ERROR: Iterator stopped after #%u instead of #%u\n",
- idx - 1, 99 - jdx);
- }
- (&iter)->Reset();
- if ((num = (TNumber*)(&iter)->Next()) != NULL)
- Printf("ERROR: Iterator return #%u when list should be empty\n",
- num->fNumber);
- TestIsEmpty(true);
- TestCount(0);
- TestFirstLast(-1, -1);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
-
- fTest->RemoveAll(); // Just in case
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLinkLast(link);
- }
- if (verbose)
- Printf("INFO: Testing RemoveCurrentObject in the middle\n");
- matchFirst = 49;
- matchLast = 50;
- for (jdx = 0; jdx < 100; ++jdx)
- {
- (&iter)->Reset();
- if (jdx & 1)
- idx = matchFirst;
- else
- idx = matchLast;
- while ((num = (TNumber*)(&iter)->Next()) != NULL)
- {
- if (num->fNumber > matchFirst && num->fNumber < matchLast)
- Printf("ERROR: Iterator returned #%u when it should be gone\n",
- num->fNumber);
- if (idx == num->fNumber && !(&iter)->RemoveCurrentObject())
- Printf("ERROR: RemoveCurrentObject returned false for #%u\n",
- idx);
- }
- if (jdx & 1)
- matchFirst -= 1;
- else
- matchLast += 1;
- }
- (&iter)->Reset();
- if ((num = (TNumber*)(&iter)->Next()) != NULL)
- Printf("ERROR: Iterator return #%u when list should be empty\n",
- num->fNumber);
-
- TestIsEmpty(true);
- TestCount(0);
- TestFirstLast(-1, -1);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
- }
-
- if (verbose)
- Printf("INFO: Testing DeleteAll\n");
-
- fTest->RemoveAll(); // Just in case
-
- for (idx = 0; idx < 100; ++idx)
- {
- link = fArray[idx];
- fTest->AddLinkLast(link);
- }
-
-
- fTest->DeleteAll(kTDynamicPointer);
-
- TestIsEmpty(true);
- TestCount(0);
- TestFirstLast(-1, -1);
- for (jdx = 0; jdx < 100; ++jdx)
- TestMember(jdx, true);
- }
-
- /**********************************************************************
- ** PUBLIC EndTest
- ***********************************************************************/
-
- void TTestLinkedList::EndTest(BooleanParm verbose, BooleanParm)
- {
- if (verbose)
- Printf("INFO: End of Linked List test\n");
- for (short idx = 0; idx < 100; ++idx)
- {
- TLink* link = fArray[idx];
- delete link;
- }
- if (verbose)
- Printf("INFO: Deleted 100 links\n");
- }
-